package com.yaoweigroup.community.adapter;

import java.util.List;

import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Constant;
import com.yaoweigroup.community.enity.SportShop;
import com.yaoweigroup.community.enity.UserInfo;
import com.yaoweigroup.community.ui.home.SportBookActivity;
import com.yaoweigroup.community.util.SharePreferenceUtil;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SportAdapter extends BaseAdapter {

	private Context context;
	private List<SportShop> list;
	private LayoutInflater mInflater;
	private UserInfo userInfo;
	private SharePreferenceUtil util;

	public SportAdapter(Context context, List<SportShop> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;
		util = new SharePreferenceUtil(context, Constant.prf);
		userInfo = (UserInfo) util.loadObject("user");
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.adapter_sport_item, null);
			convertView.setTag(holder);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.ok_btn = (Button) convertView.findViewById(R.id.ok_btn);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.price_tv);
			holder.type_tv = (TextView) convertView.findViewById(R.id.type_tv);
			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);

		} else {
			holder = (Holder) convertView.getTag();
		}

		if (userInfo != null && userInfo.getIsactivitie() == 1) {
			holder.ok_btn.setVisibility(View.VISIBLE);
		} else {
			holder.ok_btn.setVisibility(View.GONE);
		}

		final SportShop shop = list.get(position);
		holder.name_tv.setText(shop.getSeatNo());
		holder.price_tv.setText("每小时:" + shop.getCostPerHour() + " 元");
		holder.type_tv.setText("类型:" + shop.getSiteType());
		holder.time_tv.setText("开始时间:" + shop.getBeginTime() + " ,结束时间:"
				+ shop.getEndTime());

		holder.ok_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(context, SportBookActivity.class);
				intent.putExtra("guid", shop.getGuid());
				context.startActivity(intent);
				// Toast.makeText(context, "开发中，请稍等", 500).show();
			}
		});
		return convertView;
	}

	public final class Holder {

		TextView name_tv;
		ImageView icon_iv;
		Button ok_btn;
		TextView price_tv;
		TextView type_tv;
		TextView time_tv;
	}

	public void setDataChange(List<SportShop> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
